'''
''' IBAN Functions - IBANCountryDef.vb
''' 
''' This program is free software; you can redistribute it and/or modify
''' it under the terms of the GNU General Public License as published by
''' the Free Software Foundation; either version 2 of the License, or
''' (at your option) any later version.
'''
''' This program is distributed in the hope that it will be useful,
''' but WITHOUT ANY WARRANTY; without even the implied warranty of
''' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
''' GNU General Public License for more details.
'''
''' You should have received a copy of the GNU General Public License
''' along with this program; if not, write to the Free Software
''' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''

Imports System.Xml

''' -----------------------------------------------------------------------------
''' Project	 : IBANFuncsCore
''' Class	 : Core.IBANCountryDef
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' This class provides the blueprint for storing a country's IBAN rules.
''' </summary>
''' <remarks>
''' The IBAN is structured differently in each country. Allthough the IBAN has always
''' the same basic elements (country, bank and branch code, account number and check
''' digits) the length of some elements like the account number varies. In addition,
''' some countries allow the use of alphanumeric characters in accounts numbers or
''' bank codes while others don't.
''' </remarks>
''' <history>
''' 	[Nick]	2/8/2007	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class IBANCountryDef

#Region "Working variables"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Internal representation of the country code.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private _CountryCode As String

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Internal representation of the country name.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private _CountryName As String

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Internal representation of the IBAN length.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private _IBANLength As Integer

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Internal representation for the alphanumeric character positions.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private _CharPositions() As Integer = {}

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Internal representation for the bank code length.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private _BankCodeLen As Integer

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Internal representation for the flag allowing alphanumeric characters in
    ''' the bank code.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private _BankCodeAllowAlpha As Boolean

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Internal representation of the branch code length.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private _BranchCodeLen As Integer

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Internal representation for the flag allowing alphanumeric characters in 
    ''' the branch code.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private _BranchCodeAllowAlpha As Boolean

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Internal representation of the account number length.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private _AccountNumberLen As Integer

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Internal representation for the flag allowing alphanumeric characters in 
    ''' the account number.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private _AccountNumberAllowAlpha As Boolean

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Internal representation of the comments.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private _Comments As String

#End Region

#Region "Properties"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Property to get/set the country code.
    ''' </summary>
    ''' <value>String representation of the country code.</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Property CountryCode() As String
        Get
            Return _CountryCode
        End Get
        Set(ByVal Value As String)
            _CountryCode = Value
        End Set
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Property to get/set the country name.
    ''' </summary>
    ''' <value>String representation of the country name.</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Property CountryName() As String
        Get
            Return _CountryName
        End Get
        Set(ByVal Value As String)
            _CountryName = Value
        End Set
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Property to get/set the length of the IBAN for the country represented
    ''' by this instance.
    ''' </summary>
    ''' <value>Length of the IBAN.</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Property IBANLength() As Integer
        Get
            Return _IBANLength
        End Get
        Set(ByVal Value As Integer)
            _IBANLength = Value
        End Set
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Property to get/set the length of the bank code.
    ''' </summary>
    ''' <value>Length of the bank code.</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Property BankCodeLength() As Integer
        Get
            Return _BankCodeLen
        End Get
        Set(ByVal Value As Integer)
            _BankCodeLen = Value
        End Set
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Property to get/set whether the bank code for the country represented
    ''' by this instance can have alphanumeric characters.
    ''' </summary>
    ''' <value>True/False.</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Property BankCodeAlpha() As Boolean
        Get
            Return _BankCodeAllowAlpha
        End Get
        Set(ByVal Value As Boolean)
            _BankCodeAllowAlpha = Value
        End Set
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Property to get/set the length of the branch code.
    ''' </summary>
    ''' <value>Length of the branch code.</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Property BranchCodeLength() As Integer
        Get
            Return _BranchCodeLen
        End Get
        Set(ByVal Value As Integer)
            _BranchCodeLen = Value
        End Set
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Property to get/set whether the branch code for the country 
    ''' represented by this instance can have alphanumeric characters.
    ''' </summary>
    ''' <value>True/False.</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Property BranchCodeAlpha() As Boolean
        Get
            Return _BranchCodeAllowAlpha
        End Get
        Set(ByVal Value As Boolean)
            _BranchCodeAllowAlpha = Value
        End Set
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Property to get/set the length of the account number.
    ''' </summary>
    ''' <value>Length of the account number.</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Property AccountNumberLength() As Integer
        Get
            Return _AccountNumberLen
        End Get
        Set(ByVal Value As Integer)
            _AccountNumberLen = Value
        End Set
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Property to get/set whether the account number for the country represented
    ''' by this instance can have alphanumeric characters.
    ''' </summary>
    ''' <value>True/False.</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Property AccountNumberAlpha() As Boolean
        Get
            Return _AccountNumberAllowAlpha
        End Get
        Set(ByVal Value As Boolean)
            _AccountNumberAllowAlpha = Value
        End Set
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Property to get/set the comments for this country.
    ''' </summary>
    ''' <value>String with comments.</value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Property Comments() As String
        Get
            Return _Comments
        End Get
        Set(ByVal Value As String)
            _Comments = Value
        End Set
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Property to get/set the array of positions for which IBAN codes of the country
    ''' represented by this instance <b>must</b> have non-numeric characters.
    ''' </summary>
    ''' <value></value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Property CharPositions() As Integer()
        Get
            Return _CharPositions
        End Get
        Set(ByVal Value As Integer())
            _CharPositions = Value
        End Set
    End Property

#End Region

#Region "Constructors"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Default constructor.
    ''' </summary>
    ''' <param name="x">XML node with country definition.</param>
    ''' <remarks>
    ''' For more information, see the IBANDefinitions.xml file.
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub New(ByVal x As XmlNode)

        For i As Integer = 0 To x.ChildNodes.Count - 1

            If x.ChildNodes(i).NodeType = Xml.XmlNodeType.Element Then

                Dim sName As String = x.ChildNodes(i).Name.ToUpper
                Select Case sName
                    Case "COUNTRYCODE"
                        _CountryCode = x.ChildNodes(i).Attributes("value").Value
                    Case "COUNTRYNAME"
                        _CountryName = x.ChildNodes(i).Attributes("value").Value
                    Case "IBANLENGTH"
                        _IBANLength = Convert.ToInt32(x.ChildNodes(i).Attributes("value").Value)
                    Case "CHARPOSITIONS"
                        Dim s As String = x.ChildNodes(i).Attributes("value").Value
                        If s <> "" Then
                            Dim sSplit() As String = s.Split(",")
                            For j As Integer = 0 To sSplit.GetUpperBound(0)
                                ReDim Preserve _CharPositions(j)
                                _CharPositions(j) = Convert.ToInt32(sSplit(j))
                            Next
                        End If
                    Case "BANKCODE"
                        _BankCodeLen = Convert.ToInt32(x.ChildNodes(i).Attributes("length").Value)
                        _BankCodeAllowAlpha = Convert.ToBoolean(x.ChildNodes(i).Attributes("allowChars").Value)
                    Case "BRANCHCODE"
                        _BranchCodeLen = Convert.ToInt32(x.ChildNodes(i).Attributes("length").Value)
                        _BranchCodeAllowAlpha = Convert.ToBoolean(x.ChildNodes(i).Attributes("allowChars").Value)
                    Case "ACCOUNTNUMBER"
                        _AccountNumberLen = Convert.ToInt32(x.ChildNodes(i).Attributes("length").Value)
                        _AccountNumberAllowAlpha = Convert.ToBoolean(x.ChildNodes(i).Attributes("allowChars").Value)
                    Case "COMMENTS"
                        _Comments = x.ChildNodes(i).Attributes("value").Value
                End Select

            End If

        Next

    End Sub

#End Region

#Region "Methods"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Returns the number of non-numeric characters that must be present in
    ''' an IBAN for a country represented by this instance.
    ''' </summary>
    ''' <returns>Number of mandatory non-numeric characters.</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function CharPositionsNum() As Integer
        Return _CharPositions.GetUpperBound(0)
    End Function

#End Region

End Class